#!/usr/bin/perl -w	# useful for syntax highlighting
package clreqd_modules;

use cldb;
use pidlock;
use C_client2;
use Sys::Hostname;

#########################################################################
# This utility is used to start the specified daemon on the local 	#
# machine if it is not already running.					#
#									#
# Output is:								#
# 	OK [RUNNING/STARTED]						#
# or:   FAIL [msg]							#
#									#
#########################################################################

sub daemon_start {
my %ARGS=@_;
my @D=qw(hbd lockd netd lockd syncd statd mond clusterd);

	my $args="";
	my $d=$ARGS{DAEMON};
	$args=$ARGS{ARGS} if exists($ARGS{ARGS});

	my @qu=split(/,/,$d);
	for (@qu) {
		s/^lha_//;
		$_.="d" if ! /d$/;
	}
	
	for $d (@qu) {
		if(!scalar(grep {/^$d$/} @D)) {
			return "FAIL Daemon '$d' is unknown.";
		}
	}
	my $cldb=undef;

	my @r=();
	my $pl=new pidlock($d);
	if(!defined($pl)) {
		return "FAIL Unable to create pidlock object:\n" . pidlock::error;
	}
	my $r=$pl->islocked;
	if($r==-1) {
		return "FAIL Unable to check for lock object:\n" . pidlock::error;
	}
	if($r==0) {
		#################################################
		# No lock, then check PING request, just 	#
		# in case no lock file was created.		#
		#################################################
		if(!defined($cldb)) {
			$cldb=new cldb("$::ENV{TRUECL_CFG}/config.db");
			if(!defined($cldb)) {
				::error("Unable to open database:\n" . cldb::error);
			}
		}
		if(defined($cldb)) {
			my ($client,$port);
			my $h=$cldb->get_rows("SELECT number FROM ports WHERE detail = 'lha_$d';");
			if(!defined($h) || !defined($h->[0])) {
				::error("Port not found for 'lha_$d' in config database.");
			} else {
				$port=$h->[0]->{number};
				$client=new C_client2(SERVER_NAME => hostname,SERVER_INET => "127.0.0.1",
						SERVER_PORT=>$port, PKI=>$::PKI);
				if(!defined($client)) {
					::error("Unable to gain connection to 'lha_$d'.");
				} else {
					$client->set_params(TIMEOUT => 2);

				#########################################################################
				# We have a connection, so send a message via it and wait for the	#
				# response.								#
				#########################################################################

					my ($resp,$err)=$client->send_request("PING");
					if(defined($resp) && $resp =~ /OK/) {
						$r=1;
					}
				}
			}
		}
	}
	$cldb->disconnect() if defined($cldb);
	if($r) {
		return "OK RUNNING";
	}

	#################################################################
	# If we get here we can attempt to start the daemon.		#
	#################################################################

	my $cmd="$::ENV{TRUECL_CODE}/bin/lha_$d";
	if(! -f $cmd) {
		return "FAIL Can not find program '$cmd'.";
	}

	#################################################################
	# We always start in the background, so scrub out --nodetach	#
	# if included in the arguments.					#
	#################################################################
	$args =~ s/--nodetach//g;
	system("$cmd $args &");
	::msg("Started '$cmd $args'.");
	return "OK STARTED";
}

1;
